package com.wind.plugin.annotator;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlText;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.wind.plugin.common.Common.*;
import static com.wind.plugin.common.Constant.GIT_DATE;
import static com.wind.plugin.window.Utils.annotateRange;

public class MapperXmlAnnotator implements Annotator {

    private static final Pattern CUR_PATTERN = Pattern.compile("\\bCUR\\b|\\bcurr\\b|" +
            "\\bCUR_CODE\\b|\\bE_CODE\\b|\\bNBCB_CUR_CODE\\b|\\bCURRENCY_ID\\b", Pattern.CASE_INSENSITIVE);

    @Override
    public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
        // Check if the element is an XmlTag
        if (element instanceof XmlTag) {
            XmlTag tag = (XmlTag) element;
            String filePath = "";
            PsiElement psiFile = PsiTreeUtil.getParentOfType(element, PsiElement.class);
            if (psiFile != null) {
                filePath = psiFile.getContainingFile().getVirtualFile().getPath();
            }

            // Check if the tag is a DML tag (like <insert>, <update>, <delete>, etc.)
            if (isDmlTag(tag)) {
                // Iterate over the children of the tag
                PsiElement[] children = tag.getChildren();
                for (PsiElement child : children) {
                    if (child instanceof XmlText) {
                        processText(element, child, holder, filePath);
                    }
                }
            }
        }
    }


    private void processText(PsiElement element, PsiElement child,
                             AnnotationHolder holder, String filePath) {
        // Find CUR or curr in the plain text of the DML statement
        PsiFile containingFile = element.getContainingFile();
        Document document = containingFile.getViewProvider().getDocument();
        assert document != null;

        String text = child.getText();
        Matcher matcher = getCurPattern().matcher(text);
        int startOffset = child.getTextRange().getStartOffset();
        while (matcher.find()) {
            // Highlight each occurrence of CUR or curr
            // Calculate the start and end offsets of the match
            int start = startOffset + matcher.start();
            int end = startOffset + matcher.end();
            int lineNumber = document.getLineNumber(end) + 1;
            // 获取当前元素所在项目的 Git 仓库路径
            String gitRepoPath = getGitRepositoryPath(element.getProject());
            boolean shouldAnnotate;
            try{
                shouldAnnotate = isCommitAfterDate(gitRepoPath, filePath, lineNumber, GIT_DATE);
            } catch (Exception e) {
                shouldAnnotate = true;
            }

            if (shouldAnnotate){
                annotateRange(start, end, holder,"币种项需注意");
            }
        }
    }


    private boolean isDmlTag(XmlTag tag) {
        String tagName = tag.getName();
        switch (tagName.toLowerCase()) {
            case "insert":
            case "update":
            case "delete":
            case "select":
            case "sql":
            case "resultmap":
            case "if":
            case "foreach":
                return true;
            default:
                return false;
        }
    }

}

